package main

import (
	"fmt"
)

type ListNode struct {
	Data int
	Next *ListNode
}

type List struct {
	Len  int
	Head *ListNode
}

func NewList() *List {
	return &List{Len: 0}
}

func Insert(root *List, data int, address int) {
	insertN := root.Head
	if insertN == nil {
		root.Head = new(ListNode)
		root.Head.Data = data
		root.Head.Next = nil
		return
	}
	before := insertN
	for i := 0; i < address; i++ {
		before = insertN
		insertN = insertN.Next
	}
	node := new(ListNode)
	node.Data = data
	if insertN == root.Head {
		node.Next = insertN
		root.Head = node
		return
	}
	//if insertN==nil{
	node.Next = insertN
	before.Next = node
	return
	//}
	//node.Next = insertN.Next
	//insertN.Next = node
	//root.Len += 1
}

func GetArr(root *List) []int {
	result := []int{}
	node := root.Head
	for node != nil {
		result = append(result, node.Data)
		node = node.Next
	}
	return result
}

func createTargetArray(nums []int, index []int) []int {
	list := NewList()
	l := len(nums)
	for i := 0; i < l; i++ {
		Insert(list, nums[i], index[i])
	}
	result := GetArr(list)
	return result
}

func main() {
	nums := []int{1, 2, 3, 4, 0}
	index := []int{0, 1, 2, 3, 0}
	result := createTargetArray(nums, index)
	fmt.Println(result)
}
